<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Api\Question;
use Illuminate\Http\Request;
use App\Http\Controllers\BaseController;
use App\Http\Controllers\utils\Common;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

/**
 * 需要执行纯 sql语句的话 就是直接 DB::select update 等等
 * leg：修改
 *    $sql = "update questions set $column = :value where id = :id";
 *    $updateData = DB::update($sql, ['value' => $value, 'id' => $id]);
 */

class QuestionController extends BaseController
{
    function __construct()
    {
        $this->common = new Common();
    }

    /**
     * Display a listing of the resource.
     *
     * /api/v1/question GET
     * 随机获取40条问题
     */
    public function index(Request $request)
    {
        $params = $request->all();
        $limit = isset($params['limit']) ? $params['limit']  : 10;
        $type = isset($params['type']) ? $params['type'] : -1;
        $sourceid = isset($params['sourceid']) ? $params['sourceid'] : 1;
        // $sql = "select * from questions order by rand() limit 40";
        $sql = "
            (select * from questions where type = '1' order by rand() limit 20) union
            (select * from questions where type = '2' order by rand() limit 10) union
            (select * from questions where type = '3' order by rand() limit 10)
        ";
        // $questions = Question::paginate($limit);
        // $questions = Question::select($sql);

        // $questions = collect([]);
        // $questions = $questions->merge(Question::where('type', '1')->inRandomOrder()->limit(20)->get());
        // $questions = $questions->merge(Question::where('type', '2')->inRandomOrder()->limit(10)->get());
        // $questions = $questions->merge(Question::where('type', '3')->inRandomOrder()->limit(10)->get());
        $questions = [];
        // ->where('sourceid', $sourceid) 先暂时不考虑题源 -1就是获取随机40题考试或者模拟的，这样等一会题多了再考虑分题库考试
        if ($type == -1) {
            // $questions = Question::inRandomOrder()
            //     ->where('type', '1')->limit(20)
            //     ->orWhere(function ($query) {
            //         $query->where('type', '2')->limit(10);
            //     })
            //     ->orWhere(function ($query) {
            //         $query->where('type', '3')->limit(10);
            //     })
            //     // ->orWhere(function ($query) {
            //     //     $query->where('type', '4')->limit(5);
            //     // })
            //     ->where('status', 1)
            //     ->get();
            $list = ['id', 'title', 'options', 'type', 'detail'];
            $questions = Question::inRandomOrder()
                ->where('type', '1')->limit(20)
                ->select($list)
                ->union(
                    Question::where('type', '2')->inRandomOrder()->limit(10)->select($list)
                )
                ->union(
                    Question::where('type', '3')->inRandomOrder()->limit(10)->select($list)
                )
                // ->union(
                //     Question::where('type', '4')->inRandomOrder()->limit(5)
                // )
                ->where('status', 1)
                ->get();
        } else {
            $questions = Question::where('status', 1)->where('sourceid', $sourceid)->where('type', $params['type'])->limit($limit)->simplePaginate($limit);
        }
        // 将 options反序列化
        $questions->transform(function ($questions) {
            $questions->options = json_decode($questions->options);
            return $questions;
        });
        return $this->responseData($questions, '获取成功', 200);
    }

    /**
     * Store a newly created resource in storage.
     *
     * api/v1/question POST
     * 插入问题
     */
    public function store(Request $request)
    {
        $validateList = [
            'title' => 'required',
            'options' => 'required',
            'content' => 'required',
            'type' => 'required',
            'detail' => 'required',
            'sourceid' => 'required|numeric',
            'answer' => 'required',
            'score' => 'required',
            'status' => 'required'
        ];
        return $this->common->apiAdd(Question::class, $request, $validateList);
    }

    /**
     * Display the specified resource.
     *
     * api/v1/question/id GET
     */
    public function show(string $id)
    {
        $this->responseData([], '无数据', 204);
    }

    /**
     * Update the specified resource in storage.
     *
     * api/v1/question/id  PUT
     */
    public function update(Request $request, string $id)
    {
        // 数据库
        $db = DB::table("questions");
        // $sql = "update questions set $column = :value where id = :id";
        // $updateData = DB::update($sql, ['value' => $value, 'id' => $id]);

        return $this->common->apiEdit(Question::class, $request, $id);
    }

    /**
     * Remove the specified resource from storage.
     *
     * api/v1/question/id  DELETE
     */
    public function destroy(Request $request, string $id)
    {
        return $this->common->apiDelete(Question::class, $request, $id);
    }
}
